package Playlist
{
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	
	import mx.collections.ArrayCollection;
	import Player.PlayerEvent;
	
	public class Playlist extends EventDispatcher
	{
		public var db_id:Number = -1;
		private var songURLs:ArrayCollection = new ArrayCollection();
		public var currentIndex:Number = -1;     
		public var songName:String = "";
		public var nextSong:String = "";
		public var prevSong:String = "";
		
		
		public function Playlist()
		{
			super();
		}
		
		public function setCurrentIndex(i:int) : void
		{
			currentIndex = i;
			updateSongsName();
		}

		public function length() : int
		{
			return songURLs.length;
		}
			
		public function getsongName(index:int) : String
		{
			return songURLs[index];
		}
		
		public function onProgress(event:PlayerEvent) : void
		{
			dispatchEvent(event);
		}
		
		public function AddSong(path:String) : void
		{
			songURLs.addItem(path);
			if (currentIndex == -1)
				currentIndex = 0;
			updateSongsName();
		}
		
		public function FlushAllSongs() : void
		{
			songURLs.removeAll();
		}
		
		private function updateSongsName():void
		{
			songName = songURLs[currentIndex];
			if (currentIndex == 0)
				prevSong = songURLs[songURLs.length - 1];
			else
				prevSong = songURLs[currentIndex - 1];
			if (currentIndex == songURLs.length - 1)
				nextSong = songURLs[0];
			else
				nextSong = songURLs[currentIndex + 1];
		}
		
		// Switch to Next Song
		public function next():void
		{
			if (currentIndex == -1)
				return;
			currentIndex++;
			if (currentIndex >= songURLs.length)
			{
				currentIndex = 0;
			}     
			updateSongsName();
		}    
		
		// Switch to Previous Song
		public function prev():void
		{
			if (currentIndex == -1)
				return;
			currentIndex--;
			if (currentIndex < 0) {
				currentIndex = songURLs.length - 1;
			}    
			updateSongsName();
		}
		
		public function deleteSong(index:int) : void
		{
			songURLs.removeItemAt(index);
		}
	}
}